home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / multip1a / frmmain.frm (.txt) next >
Encoding:
Visual Basic Form  |  1999-10-21  |  34.5 KB  |  942 lines

  1. VERSION 5.00
  2. Object = "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.2#0"; "RICHTX32.OCX"
  3. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  4. Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
  5. Begin VB.Form frmMain 
  6.    Caption         =   "Multi-User Rich Text IP Chat"
  7.    ClientHeight    =   3660
  8.    ClientLeft      =   60
  9.    ClientTop       =   345
  10.    ClientWidth     =   6045
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   3660
  13.    ScaleWidth      =   6045
  14.    StartUpPosition =   3  'Windows Default
  15.    Begin VB.ListBox lstUsers 
  16.       BeginProperty Font 
  17.          Name            =   "Arial"
  18.          Size            =   9
  19.          Charset         =   0
  20.          Weight          =   400
  21.          Underline       =   0   'False
  22.          Italic          =   0   'False
  23.          Strikethrough   =   0   'False
  24.       EndProperty
  25.       Height          =   1890
  26.       Left            =   4890
  27.       Sorted          =   -1  'True
  28.       TabIndex        =   19
  29.       Top             =   195
  30.       Width           =   1095
  31.    End
  32.    Begin MSWinsockLib.Winsock sckConnect 
  33.       Index           =   0
  34.       Left            =   6360
  35.       Top             =   3240
  36.       _ExtentX        =   741
  37.       _ExtentY        =   741
  38.       _Version        =   393216
  39.    End
  40.    Begin RichTextLib.RichTextBox rtbChat 
  41.       Height          =   1890
  42.       Left            =   75
  43.       TabIndex        =   18
  44.       Top             =   195
  45.       Width           =   4815
  46.       _ExtentX        =   8493
  47.       _ExtentY        =   3334
  48.       _Version        =   393217
  49.       Enabled         =   -1  'True
  50.       ReadOnly        =   -1  'True
  51.       ScrollBars      =   2
  52.       TextRTF         =   $"frmMain.frx":0000
  53.    End
  54.    Begin MSComDlg.CommonDialog dlgColors 
  55.       Left            =   6720
  56.       Top             =   1920
  57.       _ExtentX        =   847
  58.       _ExtentY        =   847
  59.       _Version        =   393216
  60.    End
  61.    Begin VB.CommandButton cmdColors 
  62.       Height          =   320
  63.       Left            =   3120
  64.       Picture         =   "frmMain.frx":00C1
  65.       Style           =   1  'Graphical
  66.       TabIndex        =   17
  67.       Top             =   2175
  68.       Width           =   315
  69.    End
  70.    Begin RichTextLib.RichTextBox rtbText 
  71.       Height          =   360
  72.       Left            =   0
  73.       TabIndex        =   16
  74.       Top             =   2475
  75.       Width           =   5400
  76.       _ExtentX        =   9525
  77.       _ExtentY        =   635
  78.       _Version        =   393217
  79.       Enabled         =   -1  'True
  80.       TextRTF         =   $"frmMain.frx":0403
  81.       BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
  82.          Name            =   "Arial"
  83.          Size            =   9.75
  84.          Charset         =   0
  85.          Weight          =   400
  86.          Underline       =   0   'False
  87.          Italic          =   0   'False
  88.          Strikethrough   =   0   'False
  89.       EndProperty
  90.    End
  91.    Begin VB.CheckBox chkUnderline 
  92.       Height          =   320
  93.       Left            =   4200
  94.       Picture         =   "frmMain.frx":04C4
  95.       Style           =   1  'Graphical
  96.       TabIndex        =   15
  97.       Top             =   2175
  98.       Width           =   315
  99.    End
  100.    Begin VB.CheckBox chkItalic 
  101.       Height          =   320
  102.       Left            =   3900
  103.       Picture         =   "frmMain.frx":0806
  104.       Style           =   1  'Graphical
  105.       TabIndex        =   14
  106.       Top             =   2175
  107.       Width           =   315
  108.    End
  109.    Begin VB.ComboBox cmbFonts 
  110.       BeginProperty Font 
  111.          Name            =   "Arial"
  112.          Size            =   8.25
  113.          Charset         =   0
  114.          Weight          =   400
  115.          Underline       =   0   'False
  116.          Italic          =   0   'False
  117.          Strikethrough   =   0   'False
  118.       EndProperty
  119.       Height          =   330
  120.       Left            =   10
  121.       Sorted          =   -1  'True
  122.       Style           =   2  'Dropdown List
  123.       TabIndex        =   13
  124.       Top             =   2175
  125.       Width           =   3000
  126.    End
  127.    Begin VB.CheckBox chkBold 
  128.       Height          =   320
  129.       Left            =   3600
  130.       Picture         =   "frmMain.frx":0B48
  131.       Style           =   1  'Graphical
  132.       TabIndex        =   12
  133.       Top             =   2175
  134.       UseMaskColor    =   -1  'True
  135.       Width           =   315
  136.    End
  137.    Begin VB.CommandButton cmdConnect 
  138.       Caption         =   "Connect"
  139.       Height          =   405
  140.       Left            =   5070
  141.       TabIndex        =   11
  142.       Top             =   2895
  143.       Width           =   975
  144.    End
  145.    Begin VB.TextBox txtIP 
  146.       Height          =   285
  147.       Left            =   2760
  148.       TabIndex        =   10
  149.       Text            =   "localhost"
  150.       Top             =   3360
  151.       Width           =   2250
  152.    End
  153.    Begin VB.TextBox txtPort 
  154.       Height          =   285
  155.       Left            =   360
  156.       TabIndex        =   8
  157.       Text            =   "400"
  158.       Top             =   3360
  159.       Width           =   2055
  160.    End
  161.    Begin VB.OptionButton optServerClient 
  162.       Caption         =   "Client"
  163.       Height          =   195
  164.       Index           =   1
  165.       Left            =   4200
  166.       TabIndex        =   6
  167.       Top             =   2995
  168.       Value           =   -1  'True
  169.       Width           =   735
  170.    End
  171.    Begin VB.OptionButton optServerClient 
  172.       Caption         =   "Server"
  173.       Height          =   195
  174.       Index           =   0
  175.       Left            =   3360
  176.       TabIndex        =   5
  177.       Top             =   2995
  178.       Width           =   855
  179.    End
  180.    Begin VB.TextBox txtNick 
  181.       Height          =   285
  182.       Left            =   440
  183.       TabIndex        =   4
  184.       Text            =   "NickName"
  185.       Top             =   2940
  186.       Width           =   2775
  187.    End
  188.    Begin VB.CommandButton cmdSend 
  189.       Caption         =   "Send"
  190.       Height          =   335
  191.       Left            =   5400
  192.       TabIndex        =   2
  193.       Top             =   2490
  194.       Width           =   625
  195.    End
  196.    Begin VB.Frame frmeSep 
  197.       Height          =   135
  198.       Left            =   0
  199.       TabIndex        =   1
  200.       Top             =   2760
  201.       Width           =   6060
  202.    End
  203.    Begin VB.Frame frmeChatWindow 
  204.       Caption         =   "Chat Window"
  205.       Height          =   2165
  206.       Left            =   0
  207.       TabIndex        =   0
  208.       Top             =   0
  209.       Width           =   6045
  210.    End
  211.    Begin VB.Shape shpGreen 
  212.       BackColor       =   &H00008000&
  213.       BackStyle       =   1  'Opaque
  214.       BorderColor     =   &H00008000&
  215.       Height          =   255
  216.       Left            =   5640
  217.       Shape           =   3  'Circle
  218.       Top             =   3360
  219.       Width           =   255
  220.    End
  221.    Begin VB.Shape shpRed 
  222.       BackColor       =   &H000000FF&
  223.       BackStyle       =   1  'Opaque
  224.       BorderColor     =   &H000000FF&
  225.       FillColor       =   &H00000080&
  226.       Height          =   255
  227.       Left            =   5160
  228.       Shape           =   3  'Circle
  229.       Top             =   3360
  230.       Width           =   255
  231.    End
  232.    Begin VB.Label lblIP 
  233.       AutoSize        =   -1  'True
  234.       BackStyle       =   0  'Transparent
  235.       Caption         =   "IP:"
  236.       Height          =   195
  237.       Left            =   2520
  238.       TabIndex        =   9
  239.       Top             =   3390
  240.       Width           =   195
  241.    End
  242.    Begin VB.Label lblPort 
  243.       BackStyle       =   0  'Transparent
  244.       Caption         =   "Port:"
  245.       Height          =   255
  246.       Left            =   0
  247.       TabIndex        =   7
  248.       Top             =   3390
  249.       Width           =   375
  250.    End
  251.    Begin VB.Line lneSep3 
  252.       BorderColor     =   &H80000003&
  253.       Index           =   1
  254.       X1              =   5025
  255.       X2              =   5025
  256.       Y1              =   2880
  257.       Y2              =   3290
  258.    End
  259.    Begin VB.Line lneSep3 
  260.       BorderColor     =   &H00FFFFFF&
  261.       Index           =   0
  262.       X1              =   5040
  263.       X2              =   5040
  264.       Y1              =   2880
  265.       Y2              =   3300
  266.    End
  267.    Begin VB.Line lneSep2 
  268.       BorderColor     =   &H80000003&
  269.       Index           =   1
  270.       X1              =   3240
  271.       X2              =   3240
  272.       Y1              =   3270
  273.       Y2              =   2880
  274.    End
  275.    Begin VB.Line lneSep 
  276.       BorderColor     =   &H80000003&
  277.       Index           =   1
  278.       X1              =   0
  279.       X2              =   5040
  280.       Y1              =   3270
  281.       Y2              =   3270
  282.    End
  283.    Begin VB.Line lneSep2 
  284.       BorderColor     =   &H00FFFFFF&
  285.       Index           =   0
  286.       X1              =   3255
  287.       X2              =   3255
  288.       Y1              =   3290
  289.       Y2              =   2880
  290.    End
  291.    Begin VB.Line lneSep 
  292.       BorderColor     =   &H00FFFFFF&
  293.       Index           =   0
  294.       X1              =   0
  295.       X2              =   5040
  296.       Y1              =   3285
  297.       Y2              =   3285
  298.    End
  299.    Begin VB.Label lblNick 
  300.       BackStyle       =   0  'Transparent
  301.       Caption         =   "Nick: "
  302.       Height          =   255
  303.       Left            =   0
  304.       TabIndex        =   3
  305.       Top             =   2970
  306.       Width           =   495
  307.    End
  308.    Begin VB.Menu mnuList 
  309.       Caption         =   "mnuList"
  310.       Visible         =   0   'False
  311.       Begin VB.Menu mnuKickUser 
  312.          Caption         =   "Kick User"
  313.          Index           =   0
  314.       End
  315.       Begin VB.Menu mnuKickUser 
  316.          Caption         =   "Kick User (why?)"
  317.          Index           =   1
  318.       End
  319.    End
  320. Attribute VB_Name = "frmMain"
  321. Attribute VB_GlobalNameSpace = False
  322. Attribute VB_Creatable = False
  323. Attribute VB_PredeclaredId = True
  324. Attribute VB_Exposed = False
  325. '**********************************************************
  326. '*     Multi User Rich Text IP Chat by Joseph Huntley     *
  327. '*               joseph_huntley@email.com                 *
  328. '*                http://joseph.vr9.com                   *
  329. '*                                                        *
  330. '*  Made:  October 21, 1999                                *
  331. '*  Level: Intermediate/Advanced                          *
  332. '**********************************************************
  333. '* Notes: I might make a regular text IP chat without all *
  334. '*        this fancy stuff, so it can be understood how   *
  335. '*        it's done better.                               *
  336. '**********************************************************
  337. Private Const vbDarkRed = &H80&
  338. Private Const vbDarkGreen = &H8000&
  339. Private strUsers() As String 'Array to hold the nick of the person connecting by index
  340. Sub AddChat(strNick As String, strRTF As String)
  341. ''Adds someone's nick and what they said to rtbChat
  342.   Dim lngLastLen As Long
  343.   ''set selected position to length of text
  344.   rtbChat.SelStart = Len(rtbChat.Text)
  345.   rtbChat.SelLength = 0
  346.   ''set the seltext to a new line plus "Nick:" and tab character
  347.   rtbChat.SelText = vbCrLf & strNick$ & ":" & vbTab
  348.   ''change color, size, font name, and font styles
  349.   rtbChat.SelStart = Len(rtbChat.Text) - (Len(strNick$) + 4) '4 = Length of vbCrLf + ':' + vbTab
  350.   rtbChat.SelLength = Len(strNick$) + 4
  351.   rtbChat.SelColor = vbBlue
  352.   rtbChat.SelFontSize = 8
  353.   rtbChat.SelFontName = "Arial"
  354.   rtbChat.SelBold = True
  355.   rtbChat.SelUnderline = False
  356.   rtbChat.SelItalic = False
  357.   ''store length of text so we can have a hangingindent later
  358.   lngLastLen& = Len(rtbChat.Text)
  359.   ''set selstart & sellength then add the rtf string
  360.   rtbChat.SelStart = lngLastLen&
  361.   rtbChat.SelLength = 0
  362.   rtbChat.SelRTF = strRTF$
  363.   ''now set the hanging indent
  364.   rtbChat.SelStart = lngLastLen&
  365.   rtbChat.SelLength = Len(rtbChat.Text) - lngLastLen&
  366.   rtbChat.SelHangingIndent = 1400
  367.   ''scroll textbox down
  368.   rtbChat.SelStart = Len(rtbChat.Text)
  369.   rtbChat.SelLength = 0
  370.   ''set focus to rtbText
  371.   rtbText.SetFocus
  372. End Sub
  373. Sub ParseData(strData As String, Index As Integer)
  374.   ''used to parse data then propery use the arguments
  375.   Dim strCommand As String, strArgument As String, strBuf1 As String
  376.   Dim strBuf2 As String, lngBuf As Long, lngBuf2 As Long, lngPos As Long, lngIndex As Long
  377.   ''store command and argument. Syntax is: '[Command] Argument'
  378.   strCommand$ = Left$(strData$, InStr(strData$, " ") - 1)
  379.   strArgument$ = Right$(strData$, Len(strData$) - InStr(strData$, " "))
  380.       Select Case UCase$(Mid$(strCommand$, 2, Len(strCommand$) - 2))
  381.          
  382.          Case "MESSAGE":
  383.             ''store nick and rtf message
  384.             strBuf1$ = Left$(strArgument$, InStr(strArgument$, ":") - 1)
  385.             strBuf2$ = Right(strArgument$, Len(strArgument$) - InStr(strArgument$, ":"))
  386.              
  387.             ''add message to rtbChat
  388.             Call AddChat(strBuf1$, strBuf2$)
  389.             
  390.          Case "SYSMSG":
  391.             ''store color and system message
  392.             strBuf1$ = Left$(strArgument$, InStr(strArgument$, ":") - 1)
  393.             strBuf2$ = Right(strArgument$, Len(strArgument$) - InStr(strArgument$, ":"))
  394.             
  395.             ''print system message
  396.             Call AddSysMessage(strBuf2$, CLng(strBuf1$))
  397.          
  398.          Case "JOIN": ''if someone new joined.
  399.          
  400.                  ''loop through listbox and check if
  401.                  ''someone is using that nick - ONLY if server
  402.                  If optServerClient(0).Value Then
  403.                      For lngIndex& = 0 To lstUsers.ListCount - 1
  404.                          If Trim(LCase$(lstUsers.List(lngIndex&))) = Trim(LCase$(strArgument$)) Then
  405.                             Call sckConnect(Index).SendData("[ERR_NICKINUSE] ")
  406.                             DoEvents
  407.                             Exit Sub
  408.                          End If
  409.                      Next lngIndex&
  410.                  End If
  411.                 
  412.              ''print "*** [Nick] has joined the chat."
  413.              Call AddSysMessage(vbCrLf & "*** " & strArgument$ & " has joined the chat.", RGB(15, 181, 0))
  414.          
  415.              ''add nick to list
  416.              Call lstUsers.AddItem(strArgument$)
  417.              strUsers(Index) = strArgument$
  418.              
  419.                 ''add all the nicks' of users in the chat, then send them to the
  420.                 ''newly connected user. - ONLY if server.
  421.                 If optServerClient(0).Value Then
  422.                      For lngIndex& = LBound(strUsers()) To UBound(strUsers())
  423.                         If strUsers(lngIndex&) <> strArgument$ And strUsers(lngIndex&) <> "" Then strBuf1$ = strBuf1$ & Chr$(1) & strUsers(lngIndex)
  424.                      Next lngIndex&
  425.                    
  426.                   ''get rid of the extra chr$(1)
  427.                   If Len(strBuf1$) Then strBuf1$ = Right$(strBuf1$, Len(strBuf1$) - 1)
  428.                   
  429.                   ''send data to user.
  430.                   Call sckConnect(Index).SendData("[User] " & strBuf1$)
  431.                   DoEvents
  432.                 End If
  433.                    
  434.          Case "LEAVE": ''user has left the chat
  435.                                
  436.                 ''get listindex of nick in listbox
  437.                 ''then remove it.
  438.                 For lngIndex& = 0 To lstUsers.ListCount - 1
  439.                    If lstUsers.List(lngIndex&) = strArgument$ Then
  440.                        Call lstUsers.RemoveItem(lngIndex&)
  441.                        Exit For
  442.                    End If
  443.                 Next lngIndex&
  444.                 
  445.              ''remove nick from strUsers()
  446.              strUsers(Index) = ""
  447.              
  448.              ''print "*** [Nick] has left the chat."
  449.              Call AddSysMessage(vbCrLf & "*** " & strArgument$ & " has left the chat.", RGB(15, 181, 0))
  450.          
  451.          Case "USER": ''receiving a user that's in the room
  452.                       ''to add to lstUsers.
  453.                  
  454.                   ''parse string so you get the users,
  455.                   ''then add then to the listbox
  456.             lngIndex& = 1
  457.             
  458.             Call lstUsers.AddItem(txtNick.Text)
  459.                   Do
  460.                     lngBuf2& = 1
  461.                     lngBuf& = InStr(lngBuf& + 1, strArgument$, Chr$(1))
  462.                     If lngBuf& = 0 Then lngBuf& = Len(strArgument$): lngBuf2& = 0
  463.                     If lngPos& = Len(strArgument$) Then Exit Do
  464.                     strBuf1$ = Mid$(strArgument$, lngPos& + 1, lngBuf& - lngPos& - lngBuf2&)
  465.                     
  466.                     ''add nick to list and strUsers()
  467.                     Call lstUsers.AddItem(strBuf1$)
  468.                     ReDim Preserve strUsers(lngIndex&) As String
  469.                     strUsers(lngIndex&) = strBuf1$
  470.                     lngIndex& = lngIndex& + 1
  471.                     
  472.                     lngPos& = lngBuf&
  473.                   Loop
  474.          
  475.          Case "KICK": ''someone was kicked by the server
  476.             ''store nick and reason
  477.             strBuf1$ = Left$(strArgument$, InStr(strArgument$, Chr$(1)) - 1)
  478.             strBuf2$ = Mid$(strArgument$, InStr(strArgument$, Chr$(1)) + 1, InStr(InStr(strArgument$, Chr$(1)) + 1, strArgument$, Chr$(1)) - InStr(strArgument$, Chr$(1)) - 1)
  479.             strbuf3$ = Right$(strArgument$, Len(strArgument$) - InStr(InStr(strArgument$, Chr$(1)) + 1, strArgument$, Chr$(1)))
  480.            
  481.             ''print "*** user has been kicked by [server] (reason)"
  482.             Call AddSysMessage(vbCrLf & "*** " & strBuf1$ & " was kicked by " & strBuf2$ & " (" & strbuf3$ & ")", RGB(15, 181, 0))
  483.          
  484.               ''delete nick from list
  485.               For lngIndex& = 0 To lstUsers.ListCount - 1
  486.                  If lstUsers.List(lngIndex&) = strBuf1$ Then
  487.                     Call lstUsers.RemoveItem(lngIndex&)
  488.                     Exit For
  489.                  End If
  490.               Next lngIndex&
  491.               
  492.               ''delete nick from strUsers
  493.               For lngIndex& = 0 To UBound(strUsers())
  494.                  If strUsers(lngIndex&) = strBuf1$ Then
  495.                     strUsers(lngIndex&) = ""
  496.                     Exit For
  497.                  End If
  498.               Next lngIndex&
  499.               
  500.          Case "ERR_NICKINUSE": ''nick is being used.
  501.               ''prompt for new nick
  502.               Do
  503.                 strBuf1$ = InputBox("The nickname '" & txtNick.Text & "' is currently in use by someone else in the chat. Please choose another nickname: ", "New nick")
  504.               Loop Until Trim(strBuf1$) <> ""
  505.               
  506.             txtNick.Text = strBuf1$
  507.             
  508.             ''change index 0 of strUsers to the person's nick.
  509.             strUsers(0) = strBuf1$
  510.             
  511.             ''resend data and exit
  512.             Call sckConnect(0).SendData("[Join] " & strBuf1$)
  513.             Exit Sub
  514.             
  515.       End Select
  516.   ''send data to everyone in the chat - ONLY if server.
  517.   If optServerClient(0).Value = True Then
  518.       For lngIndex& = 1 To sckConnect().Count - 1
  519.           If sckConnect(lngIndex&).State = sckConnected And lngIndex& <> Index Then
  520.               Call sckConnect(lngIndex&).SendData(strData$)
  521.               DoEvents
  522.           End If
  523.       Next lngIndex&
  524.   End If
  525. End Sub
  526. Sub AddSysMessage(strText As String, Optional lngColor As Long = vbRed)
  527.   'A system message is something like '*** Disconnected'
  528.   rtbChat.SelStart = Len(rtbChat.Text)
  529.   rtbChat.SelLength = 0
  530.   rtbChat.SelText = strText$
  531.   rtbChat.SelStart = Len(rtbChat.Text) - Len(strText$)
  532.   rtbChat.SelLength = Len(strText$)
  533.   rtbChat.SelColor = lngColor&
  534.   rtbChat.SelBold = False
  535.   rtbChat.SelFontName = "Courier New"
  536.   rtbChat.SelFontSize = 10
  537.   rtbChat.SelStart = Len(rtbChat.Text)
  538.   rtbChat.SelLength = 0
  539. End Sub
  540. Private Sub chkBold_Click()
  541.    'toggle bold
  542.    rtbText.SelBold = Not rtbText.SelBold
  543.    rtbText.SetFocus
  544. End Sub
  545. Private Sub chkItalic_Click()
  546.    'toggle italic
  547.    rtbText.SelItalic = Not rtbText.SelItalic
  548.    rtbText.SetFocus
  549. End Sub
  550. Private Sub chkUnderline_Click()
  551.    'toggle underline
  552.    rtbText.SelUnderline = Not rtbText.SelUnderline
  553.    rtbText.SetFocus
  554. End Sub
  555. Private Sub cmbFonts_Click()
  556.   On Error Resume Next
  557.   'set the font
  558.   rtbText.SelFontName = cmbFonts.List(cmbFonts.ListIndex)
  559.   rtbText.SetFocus
  560. End Sub
  561. Private Sub cmdColors_Click()
  562. On Error GoTo ErrorHandler
  563. dlgColors.CancelError = True
  564. dlgColors.ShowColor
  565. rtbText.SelColor = dlgColors.Color
  566. rtbText.SetFocus
  567. ErrorHandler: 'user click 'Cancel'
  568. End Sub
  569. Private Sub cmdSend_Click()
  570.   Dim lngIndex As Long
  571.     ''check if connected to someone
  572.     If sckConnect(0).State = sckClosed Then
  573.        MsgBox "Error: You must be connected to someone.", vbCritical, "Error"
  574.        Exit Sub
  575.     End If
  576.   ''send to rtbChat
  577.   Call AddChat(txtNick.Text, rtbText.TextRTF)
  578.   ''send text to server to process. - ONLY if guest
  579.   If sckConnect(0).State = sckConnected And optServerClient(1).Value Then Call sckConnect(0).SendData("[Message] " & txtNick.Text & ":" & rtbText.TextRTF)
  580.      ''send data to everyone in the chat - ONLY if server.
  581.      If optServerClient(0).Value = True Then
  582.          For lngIndex& = 1 To sckConnect().Count - 1
  583.             If sckConnect(lngIndex&).State = sckConnected Then
  584.               Call sckConnect(lngIndex&).SendData("[Message] " & txtNick.Text & ":" & rtbText.TextRTF)
  585.               DoEvents ''tell processor to finish sending
  586.                        ''data before proceeding
  587.             End If
  588.          Next lngIndex&
  589.      End If
  590.   ''clear textbox
  591.   rtbText.Text = ""
  592. End Sub
  593. Private Sub Command1_Click()
  594.    For i = 0 To UBound(strUsers())
  595.       MsgBox strUsers(i)
  596.    Next i
  597. End Sub
  598. Private Sub Form_Load()
  599.  Dim intBuffer As Integer, strFont As String
  600.    'load printer fonts to combobox
  601.    If Dir$(App.Path & "\fonts.dat") = "" Then
  602.         'font file doesnt exist. Create it.
  603.         Open App.Path & "\fonts.dat" For Output As #1
  604.              For intBuffer% = 0 To Printer.FontCount - 1
  605.                 Call cmbFonts.AddItem(Printer.Fonts(intBuffer%))
  606.                 Print #1, Printer.Fonts(intBuffer%)
  607.              Next intBuffer%
  608.         Close #1
  609.    Else
  610.         'load fonts from file
  611.         Open App.Path & "\fonts.dat" For Input As #1
  612.              While Not EOF(1)
  613.                 Input #1, strFont$
  614.                 Call cmbFonts.AddItem(strFont$)
  615.              Wend
  616.         Close #1
  617.    End If
  618.  cmbFonts.ListIndex = 0
  619.  ''cmbFonts.Sorted = True 'Alphabetize list
  620.   'set combobox to "Arial"
  621.   For intBuffer% = 0 To cmbFonts.ListCount - 1
  622.     If cmbFonts.List(intBuffer%) = "Arial" Then cmbFonts.ListIndex = intBuffer%: Exit For
  623.   Next intBuffer%
  624.   'set rtbText's font-styles
  625.   rtbText.SelBold = False
  626.   rtbText.SelUnderline = False
  627.   rtbText.SelItalic = False
  628.   rtbText.SelColor = vbBlack
  629.   rtbText.SelFontName = cmbFonts.List(cmbFonts.ListIndex)
  630.   rtbText.SelFontSize = 10
  631. End Sub
  632. Private Sub Form_Resize()
  633.   ''resize the controls on the form.
  634.     ''resize by height
  635.   rtbChat.Height = Me.Height - 2170
  636.   lstUsers.Height = Me.Height - 2170
  637.   ''set form height so it fits height of listbox
  638.   If rtbChat.Height <> lstUsers.Height And Me.WindowState = vbNormal Then Me.Height = lstUsers.Height + 2170
  639.   txtIP.Top = Me.Height - 705
  640.   txtPort.Top = Me.Height - 705
  641.   lblIP.Top = Me.Height - 675
  642.   lblPort.Top = Me.Height - 675
  643.   lneSep(0).Y1 = Me.Height - 780
  644.   lneSep(0).Y2 = Me.Height - 780
  645.   lneSep(1).Y1 = Me.Height - 790
  646.   lneSep(1).Y2 = Me.Height - 790
  647.   lneSep2(0).Y1 = Me.Height - 1185
  648.   lneSep2(0).Y2 = Me.Height - 765
  649.   lneSep2(1).Y1 = Me.Height - 1185
  650.   lneSep2(1).Y2 = Me.Height - 775
  651.   lneSep3(0).Y1 = Me.Height - 1185
  652.   lneSep3(0).Y2 = Me.Height - 765
  653.   lneSep3(1).Y1 = Me.Height - 1185
  654.   lneSep3(1).Y2 = Me.Height - 775
  655.   shpRed.Top = Me.Height - 705
  656.   shpGreen.Top = Me.Height - 705
  657.   cmdConnect.Top = Me.Height - 1170
  658.   optServerClient(0).Top = Me.Height - 1070
  659.   optServerClient(1).Top = Me.Height - 1070
  660.   lblNick.Top = Me.Height - 1095
  661.   txtNick.Top = Me.Height - 1125
  662.   frmeSep.Top = Me.Height - 1305
  663.   rtbText.Top = Me.Height - 1590
  664.   cmdSend.Top = Me.Height - 1575
  665.   cmbFonts.Top = Me.Height - 1890
  666.   cmdColors.Top = Me.Height - 1890
  667.   chkBold.Top = Me.Height - 1890
  668.   chkItalic.Top = Me.Height - 1890
  669.   chkUnderline.Top = Me.Height - 1890
  670.   frmeChatWindow.Height = Me.Height - 1920
  671.   ''do width
  672.   frmeSep.Width = Me.Width - 115
  673.   cmdConnect.Left = Me.Width - 1090
  674.   lneSep(0).X2 = Me.Width - 1125
  675.   lneSep(1).X2 = Me.Width - 1125
  676.   lneSep2(0).X1 = Me.Width - 2910
  677.   lneSep2(0).X2 = Me.Width - 2910
  678.   lneSep2(1).X1 = Me.Width - 2920
  679.   lneSep2(1).X2 = Me.Width - 2920
  680.   lneSep3(0).X1 = Me.Width - 1140
  681.   lneSep3(0).X2 = Me.Width - 1140
  682.   lneSep3(1).X1 = Me.Width - 1150
  683.   lneSep3(1).X2 = Me.Width - 1150
  684.   optServerClient(1).Left = Me.Width - 1975
  685.   optServerClient(0).Left = Me.Width - 2815
  686.   txtNick.Width = Me.Width - 3400
  687.   cmdSend.Left = Me.Width - 750
  688.   rtbText.Width = Me.Width - 750
  689.   shpGreen.Left = Me.Width - 525
  690.   shpRed.Left = Me.Width - 1005
  691.   lblIP.Left = (Me.Width - 1160) / 2
  692.   txtIP.Left = (Me.Width - 1160) / 2 + 250
  693.   txtIP.Width = (Me.Width - 1130) - ((Me.Width - 1160) / 2 + 250)
  694.   txtPort.Width = (Me.Width - 1160) / 2 - 410
  695.   frmeChatWindow.Width = Me.Width - 115
  696.   lstUsers.Left = Me.Width - 1275
  697.   rtbChat.Width = Me.Width - 1350
  698.   chkUnderline.Left = Me.Width - 1965
  699.   chkItalic.Left = Me.Width - 1965 - 315
  700.   chkBold.Left = Me.Width - 1965 - 315 * 2
  701.   cmdColors.Left = Me.Width - 3045
  702.   cmbFonts.Width = Me.Width - 3045 - 140
  703. End Sub
  704. Private Sub Form_Unload(Cancel As Integer)
  705.    ''if connected tell everyone in the chat you left.
  706.    If sckConnect(0).State = sckConnected Then Call cmdConnect_Click
  707.      
  708. End Sub
  709. Private Sub lstUsers_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  710.   ''If not right-clicking then exit
  711.   If Button <> 2 Then Exit Sub
  712.     ''Enable menu if connected
  713.     If cmdConnect.Caption = "Disconnect" Then
  714.        mnuKickUser(0).Enabled = True
  715.        mnuKickUser(1).Enabled = True
  716.     Else
  717.        mnuKickUser(0).Enabled = False
  718.        mnuKickUser(1).Enabled = False
  719.     End If
  720.   ''Pop up the menu
  721.   Call Me.PopupMenu(mnuList, vbAlignNone)
  722. End Sub
  723. Private Sub mnuKickUser_Click(Index As Integer)
  724.   Dim lngIndex As Long, lngWinsock As Long, strReason As String
  725.     ''If client then deny access to kick
  726.     If optServerClient(1).Value Then
  727.        Call AddSysMessage(vbCrLf & "*** Permission Denied")
  728.        Exit Sub
  729.     End If
  730.     ''Find winsock for user and close that connection
  731.     For lngIndex& = 0 To UBound(strUsers())
  732.          If strUsers(lngIndex&) = lstUsers.List(lstUsers.ListIndex) Then
  733.               ''Get reason
  734.               If Index Then strReason$ = InputBox("Please enter a reason why you want to kick '" & lstUsers.List(lstUsers.ListIndex) & "':", "Kick User", "<none>")
  735.               If strReason$ = "" Or strReason$ = "<none>" Then strReason$ = txtNick.Text
  736.               
  737.               Call sckConnect(lngIndex&).SendData("[SysMsg] " & RGB(15, 181, 0) & ":" & vbCrLf & "*** You were kicked by " & txtNick.Text & " (" & strReason$ & ")")
  738.               DoEvents
  739.               Call sckConnect(lngIndex&).Close
  740.               
  741.               ''clear entry in strUsers
  742.               strUsers(lngIndex&) = ""
  743.               
  744.                  ''Tell everyone that person was kicked
  745.                  For lngWinsock& = 1 To sckConnect().UBound
  746.                     If sckConnect(lngWinsock&).State = sckConnected Then
  747.                        Call sckConnect(lngWinsock&).SendData("[Kick] " & lstUsers.List(lstUsers.ListIndex) & Chr$(1) & txtNick.Text & Chr$(1) & strReason$)
  748.                        DoEvents
  749.                     End If
  750.                  Next lngWinsock&
  751.                  
  752.              ''write that nick was kicked.
  753.               Call AddSysMessage(vbCrLf & "*** " & lstUsers.List(lstUsers.ListIndex) & " was kicked by " & txtNick.Text & " (" & strReason$ & ")", RGB(15, 181, 0))
  754.               
  755.               ''remove him from list
  756.               Call lstUsers.RemoveItem(lstUsers.ListIndex)
  757.                 
  758.           End If
  759.     Next lngIndex&
  760. End Sub
  761. Private Sub rtbText_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  762.   'check if text has a certain font-style and set checkboxes
  763.   'to the current font-style
  764.    If rtbText.SelBold = True Then
  765.       chkBold.Value = vbChecked
  766.    Else
  767.       chkBold.Value = vbUnchecked
  768.    End If
  769.    If rtbText.SelItalic = True Then
  770.       chkItalic.Value = vbChecked
  771.    Else
  772.       chkItalic.Value = vbUnchecked
  773.    End If
  774.    If rtbText.SelUnderline = True Then
  775.       chkUnderline.Value = vbChecked
  776.    Else
  777.       chkUnderline.Value = vbUnchecked
  778.    End If
  779. End Sub
  780. Private Sub sckConnect_Close(Index As Integer)
  781.    'if user is guest then display "Disconnected".
  782.    If optServerClient(1).Value = True Then
  783.       cmdConnect_Click
  784.    End If
  785. End Sub
  786. Private Sub sckConnect_Connect(Index As Integer)
  787.         ''turn from red to green light
  788.         shpRed.BackColor = vbDarkRed
  789.         shpRed.BorderColor = vbDarkRed
  790.         shpGreen.BackColor = vbGreen
  791.         shpGreen.BorderColor = vbGreen
  792.         
  793.         
  794.            ''write "*** Connected". ONLY if it's the first user to connect
  795.            ''and your the server OR you just a client
  796.            If (sckConnect().UBound = 1 And optServerClient(0).Value) Or optServerClient(1).Value Then
  797.               Call AddSysMessage(vbCrLf & "*** Connected")
  798.                 
  799.                 ''if guest, then tell everyone you
  800.                 ''have joined the chat.
  801.                 If optServerClient(1).Value Then Call sckConnect(0).SendData("[Join] " & txtNick.Text)
  802.            End If
  803.         
  804. End Sub
  805. Private Sub sckConnect_ConnectionRequest(Index As Integer, ByVal requestID As Long)
  806.    Dim lngIndex As Long, blnFlag As Boolean
  807.       ''loop through winsocks and see if there is a
  808.       ''winsock that is not in use.
  809.       For lngIndex& = 1 To sckConnect().UBound
  810.          If sckConnect(lngIndex&).State = sckClosed Then
  811.              blnFlag = True
  812.              Exit For
  813.          End If
  814.       Next lngIndex&
  815.       
  816.       ''if all winsocks is in use then assign lngIndex to
  817.       ''UBound + 1, and load a new winsock.
  818.       If blnFlag = False Then
  819.          lngIndex& = sckConnect().UBound + 1
  820.          Load sckConnect(lngIndex&)
  821.          ReDim Preserve strUsers(lngIndex&) As String
  822.       End If
  823.    ''accept connection
  824.    Call sckConnect(lngIndex&).Accept(requestID&)
  825.    Call sckConnect_Connect(Index) 'raise connect event
  826. End Sub
  827. Private Sub sckConnect_DataArrival(Index As Integer, ByVal bytesTotal As Long)
  828.    Dim strData As String
  829.    ''get data
  830.    Call sckConnect(Index).GetData(strData$, vbString)
  831.          
  832.    ''parse data
  833.    Call ParseData(strData$, Index)
  834. End Sub
  835. Private Sub cmdConnect_Click()
  836.   Dim strIP As String, lngIndex As Long, strNewLine As String
  837.     If cmdConnect.Caption = "Connect" Or cmdConnect.Caption = "Listen" Then
  838.        txtPort.Enabled = False
  839.        txtNick.Enabled = False
  840.        optServerClient(0).Enabled = False
  841.        optServerClient(1).Enabled = False
  842.        cmdConnect.Caption = "Disconnect"
  843.        
  844.        ''set strUsers index 0 the nick of the person.
  845.        ReDim strUsers(0) As String
  846.        strUsers(0) = txtNick.Text
  847.     Else
  848.        txtPort.Enabled = True
  849.        txtNick.Enabled = True
  850.        optServerClient(0).Enabled = True
  851.        optServerClient(1).Enabled = True
  852.        
  853.           If optServerClient(0).Value = True Then
  854.              cmdConnect.Caption = "Listen"
  855.           Else
  856.              cmdConnect.Caption = "Connect"
  857.           End If
  858.           
  859.           ''if guest AND your connected then tell server you left the chat
  860.           If optServerClient(1).Value And sckConnect(0).State = sckConnected Then
  861.              Call sckConnect(0).SendData("[Leave] " & txtNick.Text)
  862.              DoEvents
  863.           End If
  864.         
  865.            ''loop through all winsocks and close all
  866.            ''connections.
  867.            For lngIndex& = 0 To sckConnect().UBound
  868.               If sckConnect(lngIndex&).State <> sckClosed Then Call sckConnect(lngIndex&).Close
  869.            Next lngIndex&
  870.         
  871.         ''turn from green to red light
  872.         shpRed.BackColor = vbRed
  873.         shpRed.BorderColor = vbRed
  874.         shpGreen.BackColor = vbDarkGreen
  875.         shpGreen.BorderColor = vbDarkGreen
  876.         
  877.         ''write "*** Disconnected".
  878.         Call AddSysMessage(vbCrLf & "*** Disconnected")
  879.         
  880.         ''clear user list
  881.         Call lstUsers.Clear
  882.         
  883.         ''clear user array
  884.         Erase strUsers()
  885.         Exit Sub
  886.     End If
  887.  ''if there's something in rtbChat then add
  888.  ''a new line to strNewLine - which is to be
  889.  ''sent to rtbChat.
  890.  strNewLine$ = vbCrLf
  891.  If rtbChat.Text = "" Then strNewLine$ = ""
  892.     Select Case optServerClient(0).Value
  893.         
  894.         Case True:  ''Host
  895.         
  896.            ''listen for connections
  897.            sckConnect(0).LocalPort = CLng(txtPort.Text)
  898.            sckConnect(0).Listen
  899.            
  900.            ''write "*** Waiting for Connection.", and add a new line
  901.            ''if there's something in rtbChat.
  902.            Call AddSysMessage(strNewLine$ & "*** Waiting for connection...")
  903.         
  904.            ''add nick to list
  905.            Call lstUsers.AddItem(txtNick.Text)
  906.         
  907.         Case False: ''Guest
  908.         
  909.            ''try to connect
  910.            strIP$ = txtIP.Text
  911.            If LCase$(strIP$) = "localhost" Then strIP$ = sckConnect(0).LocalIP
  912.            Call sckConnect(0).Connect(strIP$, txtPort.Text)
  913.            
  914.            ''write "*** Connecting", and add a new line
  915.            ''if there's something in rtbChat.
  916.            Call AddSysMessage(strNewLine$ & "*** Connecting...")
  917.            
  918.     End Select
  919.             
  920. End Sub
  921. Private Sub optServerClient_Click(Index As Integer)
  922.     Select Case Index
  923.        Case 0: 'Server
  924.           txtIP.BackColor = &H8000000F  'Grey
  925.           txtIP.Locked = True
  926.           cmdConnect.Caption = "Listen"
  927.           txtIP.Text = sckConnect(0).LocalIP
  928.        Case 1: 'Client
  929.           txtIP.Text = "localhost"
  930.           txtIP.BackColor = vbWhite
  931.           txtIP.Locked = False
  932.           cmdConnect.Caption = "Connect"
  933.           
  934.     End Select
  935. End Sub
  936. Private Sub rtbText_KeyPress(KeyAscii As Integer)
  937.    If KeyAscii = 13 Then 'If user pressed 'Enter'
  938.       cmdSend_Click 'click 'Send' button
  939.       KeyAscii = 0 'Make sure it doesnt write enter to rtbText
  940.    End If
  941. End Sub
  942.